package com.sec.android.app.myfiles.module.download;

import android.app.DownloadManager;
import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.download.DownloadFileRecord.DownloadType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

public class DownloadListAdapterImp extends AbsFileListAdapterImp {
    public DownloadListAdapterImp(Context context, NavigationInfo navigationInfo, AbsListViewImp listView, Cursor c, int flags) {
        super(context, navigationInfo, listView, c, flags);
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        super._bindView(context, vh, v, record, position);
        if (record instanceof DownloadFileRecord) {
            TextView description = vh.getView(FileListViewHolder.DESCRIPTION, TextView.class);
            if (description != null) {
                description.setVisibility(View.VISIBLE);
                description.setText(getDescriptionInList(record));
                if (isGridList()) {
                    TextView date = vh.getView(FileListViewHolder.DATE, TextView.class);
                    if (date != null) {
                        date.setVisibility(View.GONE);
                    }
                }
            }

            //TODO HYL DownaloadManager
            /*
            if (((DownloadFileRecord) record).getStatus() == DownloadManager.SEM_STATUS_OMA_PENDING) {
                AbsMyFilesFragment curFragment = mNavigationInfo.getCurFragment();
                if (curFragment != null) {
                    MyFilesFacade.omaConfirm(curFragment.getProcessId(), context, curFragment, record);
                } else {
                    Log.e(this, "current fragment is null");
                }
            }
            */
        }
    }

    private String getDescriptionInList(FileRecord record) {
        DownloadFileRecord downloadRecord = (DownloadFileRecord) record;
        DownloadType type = downloadRecord.getDownloadType();
        String description = downloadRecord.getDescription();
        String ret = description;
        switch (type) {
            case DOWNLOAD_BY_BROWSER:
            case DOWNLOAD_BY_MESSAGE:
            case DOWNLOAD_BY_SIMPLESHARE:
            case DOWNLOAD_BY_BLUETOOTH:
            case DOWNLOAD_BY_WIFI:
            case DOWNLOAD_BY_MTP:
                ret = mContext.getResources().getString(R.string.source_from) + " " + description;
                break;
            default:
                break;
        }

        if (ret == null) {
            ret = downloadRecord.getName();
        }

        return ret;
    }

    @Override
    public String getSelection() {
        String selection = mTableInfo.getColumnName(COLUMN_ID.DOWNLOAD_BY) + "!=" + DownloadType.DOWNLOAD_BY_MTP.ordinal();
        // for Browser - Download History
        if (getCurRecord().getFullPath().equals(AppConstants.StoragePath.DOWNLOAD_HISTORY)
                && PreferenceUtils.getSupportNotificationPackage(mContext) != -1) {
            selection += " AND " + mTableInfo.getColumnName(COLUMN_ID.FROM_S_BROWSER) + "=1";
        }
        return selection;
    }

    @Override
    public String[] getSelectionArgs() {
        return null;
    }

    @Override
    protected FileRecord _getFileRecord(Cursor cursor) {
        return getDownloadFileRecord(cursor, mTableInfo);
    }

    public static FileRecord getDownloadFileRecord(Cursor cursor, DbTableInfo tableInfo) {
        FileRecord ret = null;

        final int idIndex = tableInfo.getIndex(COLUMN_ID.ID);
        final int downloadIdIndex = tableInfo.getIndex(COLUMN_ID.DOWNLOAD_ID);
        final int pathIndex = tableInfo.getIndex(COLUMN_ID.PATH);
        final int nameIndex = tableInfo.getIndex(COLUMN_ID.NAME);
        final int sizeIndex = tableInfo.getIndex(COLUMN_ID.SIZE);
        final int dateIndex = tableInfo.getIndex(COLUMN_ID.DATE);
        final int typeIndex = tableInfo.getIndex(COLUMN_ID.FILE_TYPE);
        final int sourceIndex = tableInfo.getIndex(COLUMN_ID.SOURCE);
        final int descripIndex = tableInfo.getIndex(COLUMN_ID.DESCRIPTION);
        final int statusIndex = tableInfo.getIndex(COLUMN_ID.STATUS);
        final int downloadByIndex = tableInfo.getIndex(COLUMN_ID.DOWNLOAD_BY);

        if (cursor != null) {
            int id = cursor.getInt(idIndex);
            int downloadId = cursor.getInt(downloadIdIndex);
            String path = cursor.getString(pathIndex);
            String name = cursor.getString(nameIndex);
            long size = cursor.getLong(sizeIndex);
            long date = cursor.getLong(dateIndex);
            int fileType = cursor.getInt(typeIndex);

            String source = cursor.getString(sourceIndex);
            String descrip = cursor.getString(descripIndex);
            int status = cursor.getInt(statusIndex);
            int download = cursor.getInt(downloadByIndex);

            ret = new DownloadFileRecord(id, downloadId, path, name, size, date, fileType, source, descrip, status, download);
        }

        return ret;
    }

    @Override
    public String getFilePath(Cursor cursor) {
        return getFilePath(cursor, mTableInfo.getIndex(COLUMN_ID.PATH), mTableInfo.getIndex(COLUMN_ID.NAME));
    }

    @Override
    public StorageType getStorageType() {
        return StorageType.Downloads;
    }

    @Override
    protected boolean isDetailList(View v) {
        return true;
    }

    @Override
    public void onLoadFinished(Cursor data) {
        super.onLoadFinished(data);

        try {
            AbsMyFilesFragment fragment = mNavigationInfo.getCurFragment();
            if (fragment != null && fragment.isSelectActionMode()) {
                fragment.updateActionBarMenu();
            }
        } catch (NullPointerException e) {
            Log.e(this, e.getMessage());
        }
    }

    @Override
    public boolean _isEnabled(int position) {
        return isCheckable(position);
    }

    @Override
    public int getEmptyTextId() {
        return R.string.no_downloads;
    }
}
